package com.oocl.o2o.merchant.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oocl.o2o.merchant.dao.CommentaryDao;
import com.oocl.o2o.merchant.dao.MerchantDao;
import com.oocl.o2o.merchant.entity.Commentary;
import com.oocl.o2o.merchant.entity.Merchant;
import com.oocl.o2o.merchant.service.CommentaryManager;

@Service
public class CommentaryManagerImpl implements CommentaryManager {
	@Autowired
	private CommentaryDao commentaryDao;
	@Autowired
	private MerchantDao merchantDao;

	@Transactional
	@Override
	public Commentary reply(String commentaryId, String merchantCommentary, Date merchantCommentaryDate) {
		Commentary commentary = commentaryDao.findById(commentaryId);
		// validate
		if (commentary == null || commentary.getMerchantCommentary() != null) {
			return null;
		}
		commentary.setMerchantCommentary(merchantCommentary);
		commentary.setMerchantCommentaryDate(merchantCommentaryDate);
		return commentary;
	}

	@Transactional
	@Override
	public List<Commentary> findByMerchantId(String merchantId) {
		Merchant merchant = merchantDao.findById(merchantId);
		if (merchant == null)
			return null;
		List<Commentary> commentaries = merchant.getCommentaries();
		commentaries.size();
		return commentaries;
	}

}
